package dca;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class DiseaseRegistry {

	private ArrayList<Disease> DiseaseList = new ArrayList<Disease>();
	private String filename;
	
	public DiseaseRegistry(){};
	public DiseaseRegistry(String filename) throws IOException{
		loadDiseases(filename);
		this.filename=filename;
	}
	
	
	/**
	 * Reads a text file of diseases and loads the diseases into the registry.
	 * @return
	 */
	public void loadDiseases(String filename) throws IOException{
		File inFile = new File("data/"+filename+".txt");
		Scanner in = new Scanner(inFile);
		try
		{
		readData(in);
		}
		finally
		{
		in.close();			
		}
		
		//Scanner in = new Scanner(new File("/data/"+filename+".txt"));		
	}
	
	private void readData(Scanner in) throws BadDataException{
		while(in.hasNextLine()){
			String readName = in.nextLine();
			String CureString =in.nextLine();
			String SymptomString =in.nextLine();
			Pattern P = Pattern.compile("[,\\n]+");
			String[] CureStrings = P.split(CureString);
			String[] SymptomStrings = P.split(SymptomString);
			ArrayList<Symptom> SymptomList= new ArrayList<Symptom>();
			
			
			//This loops reads the symptom line. 
			for(int i=0;i<SymptomStrings.length;i+=2){
							SymptomList.add(new Symptom(SymptomStrings[i].trim(),SymptomStrings[i+1].trim()));
						}
			
			Cure disCure = findCure(CureStrings[0].trim(),CureStrings[1],Integer.parseInt(CureStrings[2].trim()));
			addDisease(readName, SymptomList, disCure);
			
			
		}
	}
	
	public Cure findCure(String cureType,String Name,int healFactor){
		if(cureType.equalsIgnoreCase("pill"))
		{
			PillBasedCure cure = new PillBasedCure(Name,healFactor);
			return cure;
		}
		else if(cureType.equalsIgnoreCase("injection"))
		{
			InjectionBasedCure cure = new InjectionBasedCure(Name,healFactor);
			return cure;
		}
		else if(cureType.equalsIgnoreCase("Surgery"))
		{
			SurgeryBasedCure cure = new SurgeryBasedCure(Name,healFactor);
			return cure;
		}
		throw new NoSuchElementException("No cure of type : "+cureType);
		
	}
	

	
	/**
	 * Adds a new disease to the registry if it does not already exists.
	 * @param Name
	 * @param level
	 */
	public void addDisease(String name, ArrayList<Symptom> symptoms,Cure cure){
		for(Disease d:DiseaseList)
		{
			if(d.getName().equalsIgnoreCase(name))
				return;
		}
		DiseaseList.add(new Disease(name,symptoms,cure));
	}
	
	/**
	 * Saves the current diseases as a text file.
	 * @throws FileNotFoundException 
	 */
	public void saveDiseases(String filename) throws FileNotFoundException{
		PrintWriter out = new PrintWriter("data/"+filename+".txt");
		for(Disease s:DiseaseList)
		{
			out.println(s.toString());
		}
		out.close();
	}
	public void saveDiseases() throws FileNotFoundException{
		this.saveDiseases(this.filename);
	}
	
	/**
	 * Finds a disease by name.
	 * @param Name
	 * @return
	 */
	public Disease getDisease(String Name){
		for(Disease s:DiseaseList)
			if (s.getName().equalsIgnoreCase(Name))
				return s;
		throw new NoSuchElementException("Disease "+Name+". Does not exsist in disease registry.");
	}
	
	
	/**
	 * Returns a list of all diseases in alphabetical order.
	 */
	public ArrayList<Disease> DiseaseList(){
		Collections.sort(DiseaseList);
		return DiseaseList;
	}
	
	public String getFilename(){
		return this.filename;
	}
	


	

}
